
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>FAQ: Using Django &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="FAQ: Getting Help" href="help.html" />
    <link rel="prev" title="FAQ: Installation" href="install.html" />



 
<script type="text/javascript" src="../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="install.html" title="FAQ: Installation">previous</a>
     |
    <a href="index.html" title="Django FAQ" accesskey="U">up</a>
   |
    <a href="help.html" title="FAQ: Getting Help">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="faq-usage">
            
  <div class="section" id="s-faq-using-django">
<span id="faq-using-django"></span><h1>FAQ: Using Django<a class="headerlink" href="#faq-using-django" title="Permalink to this headline">¶</a></h1>
<div class="section" id="s-why-do-i-get-an-error-about-importing-django-settings-module">
<span id="why-do-i-get-an-error-about-importing-django-settings-module"></span><h2>Why do I get an error about importing DJANGO_SETTINGS_MODULE?<a class="headerlink" href="#why-do-i-get-an-error-about-importing-django-settings-module" title="Permalink to this headline">¶</a></h2>
<p>Make sure that:</p>
<ul class="simple">
<li>The environment variable DJANGO_SETTINGS_MODULE is set to a
fully-qualified Python module (i.e. “mysite.settings”).</li>
<li>Said module is on <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> (<code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">mysite.settings</span></code> should work).</li>
<li>The module doesn’t contain syntax errors (of course).</li>
</ul>
</div>
<div class="section" id="s-i-can-t-stand-your-template-language-do-i-have-to-use-it">
<span id="i-can-t-stand-your-template-language-do-i-have-to-use-it"></span><h2>I can’t stand your template language. Do I have to use it?<a class="headerlink" href="#i-can-t-stand-your-template-language-do-i-have-to-use-it" title="Permalink to this headline">¶</a></h2>
<p>We happen to think our template engine is the best thing since chunky bacon,
but we recognize that choosing a template language runs close to religion.
There’s nothing about Django that requires using the template language, so
if you’re attached to Jinja2, Mako, or whatever, feel free to use those.</p>
</div>
<div class="section" id="s-do-i-have-to-use-your-model-database-layer">
<span id="do-i-have-to-use-your-model-database-layer"></span><h2>Do I have to use your model/database layer?<a class="headerlink" href="#do-i-have-to-use-your-model-database-layer" title="Permalink to this headline">¶</a></h2>
<p>Nope. Just like the template system, the model/database layer is decoupled from
the rest of the framework.</p>
<p>The one exception is: If you use a different database library, you won’t get to
use Django’s automatically-generated admin site. That app is coupled to the
Django database layer.</p>
</div>
<div class="section" id="s-how-do-i-use-image-and-file-fields">
<span id="how-do-i-use-image-and-file-fields"></span><h2>How do I use image and file fields?<a class="headerlink" href="#how-do-i-use-image-and-file-fields" title="Permalink to this headline">¶</a></h2>
<p>Using a <a class="reference internal" href="../ref/models/fields.html#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileField</span></code></a> or an
<a class="reference internal" href="../ref/models/fields.html#django.db.models.ImageField" title="django.db.models.ImageField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImageField</span></code></a> in a model takes a few steps:</p>
<ol class="arabic simple">
<li>In your settings file, you’ll need to define <a class="reference internal" href="../ref/settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_ROOT</span></code></a> as
the full path to a directory where you’d like Django to store uploaded
files. (For performance, these files are not stored in the database.)
Define <a class="reference internal" href="../ref/settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a> as the base public URL of that directory.
Make sure that this directory is writable by the Web server’s user
account.</li>
<li>Add the <a class="reference internal" href="../ref/models/fields.html#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileField</span></code></a> or
<a class="reference internal" href="../ref/models/fields.html#django.db.models.ImageField" title="django.db.models.ImageField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImageField</span></code></a> to your model, defining the
<a class="reference internal" href="../ref/models/fields.html#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><code class="xref py py-attr docutils literal notranslate"><span class="pre">upload_to</span></code></a> option to specify a
subdirectory of <a class="reference internal" href="../ref/settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_ROOT</span></code></a> to use for uploaded files.</li>
<li>All that will be stored in your database is a path to the file
(relative to <a class="reference internal" href="../ref/settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_ROOT</span></code></a>). You’ll most likely want to use the
convenience <a class="reference internal" href="../ref/models/fields.html#django.db.models.fields.files.FieldFile.url" title="django.db.models.fields.files.FieldFile.url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">url</span></code></a> attribute
provided by Django. For example, if your
<a class="reference internal" href="../ref/models/fields.html#django.db.models.ImageField" title="django.db.models.ImageField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImageField</span></code></a> is called <code class="docutils literal notranslate"><span class="pre">mug_shot</span></code>, you can get
the absolute path to your image in a template with
<code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">object.mug_shot.url</span> <span class="pre">}}</span></code>.</li>
</ol>
</div>
<div class="section" id="s-how-do-i-make-a-variable-available-to-all-my-templates">
<span id="how-do-i-make-a-variable-available-to-all-my-templates"></span><h2>How do I make a variable available to all my templates?<a class="headerlink" href="#how-do-i-make-a-variable-available-to-all-my-templates" title="Permalink to this headline">¶</a></h2>
<p>Sometimes your templates just all need the same thing. A common example would
be dynamically-generated menus. At first glance, it seems logical to simply
add a common dictionary to the template context.</p>
<p>The correct solution is to use a <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code>. Details on how to do this
are here: <a class="reference internal" href="../ref/templates/api.html#subclassing-context-requestcontext"><span class="std std-ref">Using RequestContext</span></a>.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">FAQ: Using Django</a><ul>
<li><a class="reference internal" href="#why-do-i-get-an-error-about-importing-django-settings-module">Why do I get an error about importing DJANGO_SETTINGS_MODULE?</a></li>
<li><a class="reference internal" href="#i-can-t-stand-your-template-language-do-i-have-to-use-it">I can’t stand your template language. Do I have to use it?</a></li>
<li><a class="reference internal" href="#do-i-have-to-use-your-model-database-layer">Do I have to use your model/database layer?</a></li>
<li><a class="reference internal" href="#how-do-i-use-image-and-file-fields">How do I use image and file fields?</a></li>
<li><a class="reference internal" href="#how-do-i-make-a-variable-available-to-all-my-templates">How do I make a variable available to all my templates?</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="install.html"
                        title="previous chapter">FAQ: Installation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="help.html"
                        title="next chapter">FAQ: Getting Help</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/faq/usage.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="install.html" title="FAQ: Installation">previous</a>
     |
    <a href="index.html" title="Django FAQ" accesskey="U">up</a>
   |
    <a href="help.html" title="FAQ: Getting Help">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>